#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>


int main()
{
    umask(0);
    int fd = open("test.txt",O_RDONLY);
   // int fd = open("test.txt",O_CREAT | O_WRONLY | O_TRUNC,0666); // 文件不存在就创建
    if(fd < 0)
    {
        perror("open");
        return 1;
    }

    while(1)
    {
        char buffer[128];
        int n = read(fd,buffer,sizeof(buffer) - 1);
        buffer[n] = 0;
        if(n > 0)
        {
            printf("%s",buffer);
        }
        else break;
    }


   // const char* msg = "abcd";
   // int a = 1234556;
   // char buffer[16];
   // snprintf(buffer,sizeof(buffer),"%d",a);
   // write(fd,buffer,strlen(buffer));
    // write(fd,msg,strlen(msg));

//    int cnt = 5;
//    const char* msg = "hello write\n";
//    while(cnt--)
//    {
//        write(fd,msg,strlen(msg));// 向文件中写的时候不需要给\0留空间
//    }

    close(fd);
    return 0;
}

//#define ONE_FLAG (1<<0)
//#define TWO_FLAG (1<<1)
//#define THREE_FLAG (1<<2)
//#define FOUR_FLAG (1<<3)
//
//void Print(int flag)
//{
//    if(flag & ONE_FLAG)
//    {
//        printf("One\n");
//    }
//    if(flag & TWO_FLAG)
//    {
//        printf("Two\n");
//    }
//    if(flag & THREE_FLAG)
//    {
//        printf("Three\n");
//    }
//    if(flag & FOUR_FLAG)
//    {
//        printf("Four\n");
//    }
//}
//
//int main()
//{
//    Print(ONE_FLAG);
//    printf("\n");
//    Print(ONE_FLAG | TWO_FLAG);
//    printf("\n");
//    Print(ONE_FLAG | TWO_FLAG | THREE_FLAG);
//    printf("\n");
//    Print(ONE_FLAG | TWO_FLAG | THREE_FLAG | FOUR_FLAG);
//    printf("\n");
//    Print(TWO_FLAG | FOUR_FLAG);
//    printf("\n");
//}
//
////int main()
////{
////    printf("hello printf\n");
////
////    fprintf(stdout,"hello fprintf\n");
////
////    char* msg =(char*)"hello fwrite\n";
////    fwrite(msg,strlen(msg),1,stdout);
////
////    return 0;
////}
////
//////int main(int argc,char* argv[])
//////{
//////    if(argc != 2)
//////    {
//////        printf("cat filename\n");
//////        return 1;
//////    }
//////    FILE* fp = fopen(argv[1],"r");
//////    if(fp == NULL) 
//////    {
//////        perror("fopen");
//////        return 2;
//////    }
//////    // size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
//////    // 读文件
//////    while(1)
//////    {
//////        char buffer[128];
//////        memset(buffer,0,sizeof(buffer));
//////        int n = fread(buffer,1, sizeof(buffer) - 1,fp);
//////        if(n > 0)
//////        {
//////            printf("%s",buffer);
//////			fflush(stdout);
//////        }
//////        if(feof(fp)) 
//////            break;
//////    }
//////    fclose(fp);
//////    return 0;
//////}
////////
////////int main()
////////{
////////    FILE* fp = fopen("log.txt","w");
////////    if(fp == NULL)
////////    {
////////        perror("fopen");
////////        return 1;
////////    }
////////    int cnt = 1;
////////    const char* s = "Hello file";
////////    while(cnt <= 10)
////////    {
////////        char buffer[1024];
////////        snprintf(buffer,sizeof(buffer),"%s: %d\n",s,cnt++);
////////        fwrite(buffer,strlen(buffer),1,fp);
////////    }
////////
////////    fclose(fp);
////////    return 0;
////////}
